#pragma rtGlobals=1		// Use modern global access method.

Function MANIC_checkSpecList(MasterList,QueryList)
	// checks for missing, duplicate, and invalid species
	wave/t MasterList, QueryList
	
	// check wave types and abort if either is not text:
	variable AbortTest=0
	if(wavetype(MasterList,1)!=2) // if it's not a text wave
		AbortTest=1
	endif
	if(wavetype(QueryList,1)!=2) // if it's not a text wave
		AbortTest+=2
	endif		
	if(AbortTest>=1)
		if(AbortTest==1)
			DoAlert /T="FAIL!" 0, "This function works on text waves. "+nameofwave(MasterList)+" isn't a text wave."
		elseif(AbortTest>=2)
			DoAlert /T="FAIL!" 0, "This function works on text waves. Neither input waves are text waves."	
		endif
		Abort
	endif		
	
	// if all is good, start checking waves:	
	make/o/n=(numpnts(MasterList)) FreqInQueryList = 0
	note FreqInQueryList, "value gives number of times species appears in QueryList"
	make/o/n=(numpnts(QueryList)) IsDuplicate = NaN
	note IsDuplicate, "1 indicates a duplicate; 0 indicates is not a duplicate; -1 indicates duplicate, but not found in master list"
	make/o/n=(numpnts(QueryList)) PosInMasterList = NaN
	variable i
	for(i=0;i<numpnts(QueryList);i+=1)
		FindValue /TEXT=(QueryList[i]) /TXOP=2 MasterList		
		if(V_value>=0)
			FreqInQueryList[V_value]+=1
			if(FreqInQueryList[V_value]>=1) // entry found
				if (FreqInQueryList[V_value]>=2)
					IsDuplicate[i]=1
				else	
					IsDuplicate[i]=0
				endif	
				if(V_value!=i) // entry in different location to that in master list
					PosInMasterList[i]=V_value
				endif				
			endif	
		elseif (V_value==-1) //entry not found
			IsDuplicate[i]=-1
		endif	
	endfor
	// make missing and IsDuplicate list
	string MissingListStr = "", DuplicateListStr = "",InvalidListStr = ""
	for(i=0;i<numpnts(MasterList);i+=1)
		if(FreqInQueryList[i]==0)
			MissingListStr = AddListItem(MasterList[i],MissingListStr,";",inf)
		elseif(FreqInQueryList[i]>1)
			DuplicateListStr = AddListItem(MasterList[i],DuplicateListStr,";",inf)	
		endif
	endfor
	// queried entry not found in master list:
	for (i=0;i<numpnts(QueryList);i+=1)
		if (IsDuplicate[i]==-1)
			InvalidListStr = AddListItem(QueryList[i],InvalidListStr,";",inf)
		endif	
	endfor
	make/t/o/n=(itemsinlist(MissingListStr)) MissingList =  stringfromlist(p,MissingListStr)
	make/t/o/n=(itemsinlist(DuplicateListStr)) DuplicateList =  stringfromlist(p,DuplicateListStr)
	make/t/o/n=(itemsinlist(InvalidListStr)) InvalidList = stringfromlist(p,InvalidListStr)
	string TableName="SpeciesListComparisonTable"
	DoWindow $TableName
	if(V_flag==1)
		killwindow $TableName
	endif	
	edit/W=(415,44,1252,963)/k=1 MasterList,FreqInQueryList,PosInMasterList,QueryList,IsDuplicate,MissingList, DuplicateList,InvalidList
	ModifyTable format(Point)=1,width(PosInMasterList)=96
	ModifyTable font="Consolas",size=12
	DoWindow/C $TableName
End

function MCM_IDCheck(MCM_key,MCM_name,SpList,SpID)
// checks MCM 4 digit ID number ("key") is correct for species list
	wave MCM_key,SpID //SpID is the I.D. number of the species
	wave/t MCM_name,SpList //SpList is list of species names
	variable i
	make/o/n=(numpnts(SpList)) ID_check = -1
	note ID_check, "1 = correct name: 0 = incorrect name:"
	note ID_check, "-1 means ID number not found"
	variable PointNum
	for (i=0;i<numpnts(SpList);i+=1)
		PointNum = binarysearchinterp(MCM_key,SpID[i])
		if(mod(PointNum,1)==0) // if the species ID number is found
			ID_check[i]= stringmatch(MCM_name[PointNum],SpList[i])
		endif
	endfor
end

Function MCM_GetSpeciesKey(SpeciesList,MCM_SpeciesList,MCM_key)
//Gets MCM ID number for input SpeciesList
	wave/t SpeciesList,MCM_SpeciesList
	wave MCM_key
	variable i
	make/o/n=(numpnts(SpeciesList)) $NameOfWave(SpeciesList)+"_key" = NaN
	wave Species_key = $NameOfWave(SpeciesList)+"_key"
	for(i=0;i<numpnts(SpeciesList);i+=1)
		FindValue/TEXT=(SpeciesList[i])/TXOP=2 MCM_SpeciesList
		if(V_value>0)
			Species_key[i] = MCM_key[V_Value]
		else
			Species_key[i] = NaN
		endif		
	endfor
end		


function MANIC_fortran_array(SpecList,CharLen,SpPerLine)
//This function is to write an array assignment in Fortran
//SpecList = species list; CharLen = number of characters in array entry; SpPerLine = required number of species per line
	wave/t SpecList
	variable CharLen,SpPerLine		
	make/t/o/n=(numpnts(SpecList)) FSpecList = "\""+(SpecList[p])
	variable i
	for(i=0;i<numpnts(SpecList);i+=1)
		do
			FSpecList[i]+=" "
		while(strlen(FSpecList[i])<CharLen-1+2) 
		FSpecList[i]+="\""
	endfor	
	variable WvLen = ceil(numpnts(SpecList)/SpPerLine)
	string TempWvsList = ""
	for(i=0;i<SpPerLine;i+=1)	// make waves for each column
		make/t/o/n=(WvLen) $"FArrDesc"+num2str(i) = FSpecList[(p*SpPerLine)+i]
		TempWvsList+="FArrDesc"+num2str(i)
		if(i<SpPerLine)
			TempWvsList+=";"
		endif	 
	endfor
	make/t/o/n=(WvLen) FArrDesc = ""
	for(i=0;i<SpPerLine;i+=1)	// add the columns to give multiple species per line
		wave/t AddWv = $"FArrDesc"+num2str(i)
		FArrDesc+=AddWv[p]+","
		killwaves $stringfromlist(i,TempWvsList)
		if (i==SpPerLine-1)
			FArrDesc+=" &"
		endif	
	endfor	
	variable rem = mod(numpnts(SpecList),SpPerLine) 
	switch(rem)
		case 1:
			FArrDesc[numpnts(FArrDesc)-1]=FSpecList[numpnts(FSpecList)-1]+" &"
			break
		case 2	:
			FArrDesc[numpnts(FArrDesc)-1]=FSpecList[numpnts(FSpecList)-2]+", "+FSpecList[numpnts(FSpecList)-1]+" &"
			break
		case 3:
			FArrDesc[numpnts(FArrDesc)-1]=FSpecList[numpnts(FSpecList)-3]+", "+FSpecList[numpnts(FSpecList)-2]+", "+FSpecList[numpnts(FSpecList)-1]+" &"	
			break
	endswitch		
end	